var LazyDataTable = Class.extend({

	columnDefs : [],
	sortField : undefined,
	reverse : false,

	init : function(columnDefs) {
		this.columnDefs = columnDefs || [];
	},
	sort : function(fieldName, sortable) {
		var sb = sortable || false;
		if (!sb) {
			return '';
		}
		this.sortField = fieldName;
		this.reverse = !this.reverse;
	},
	sortingClass : function(fieldName, sortable) {
		var sb = sortable || false;
		if (!sb) {
			return '';
		}

		if (this.sortField == undefined
				|| this.sortField != fieldName) {
			return 'sorting';
		} else if (this.reverse) {
			return 'sorting_desc';
		} else {
			return 'sorting_asc';
		}
	},
});

var LazyPagination = Class.extend({
	data : [] ,
	init : function(settings , limit) {
		this.settings = settings;
		this.begin = 0 ;
		this.end = 0;
		this.total = 0;
		this.min = 0 ;
		this.max = 0;
		this.limit = limit || 10 ;
	} ,
	load : function(data , total , min , max) {
		this.data = data;
		this.total = total;
		this.min = min;
		this.max = max;
		this.begin  = min;
		this.end = min + this.limit;
	} ,
	lazyLoading : function(type){
		var results = this.settings['lazyLoading'](type);
		results = results || [];
		var beforeArr = type == 'next' ? this.data :  results;
		var afterArr = type == 'next' ? results : this.data;
		for (var i = 0 ; i < afterArr.length; i++) {
			beforeArr.push(afterArr[i] );
		};
		this.data = beforeArr;
	} ,
	hasNext : function(){
		return this.begin + this.limit < this.total;
	} ,
	hasPrev : function(){
		return this.begin - this.limit >= 0;
	} ,
	next : function(){
		if( this.hasNext() ) {
			this.begin += this.limit ;
			this.end += this.limit;	

			if( this.end > this.max ) {
				this.lazyLoading('next');
			}
		}
	} ,
	prev : function(){
		if( this.hasPrev() ) {
			this.begin -= this.limit;
			this.end -= this.limit;	
			if( this.begin < this.min ) {
				this.lazyLoading('prev');
			}
		}
	} ,
	getData : function(){
		if(this.end > 0 && this.data.length > 0) {
			return this.data.slice(this.begin , this.end);
		} else {
			return [];
		}
		
	}
});

var LazyAlumniService = RestfulService.extend({
	data : null ,
	next : function(successcb, errorcb){
		if( this.data && this.data['linkNext']) {
			this.load( this.data['linkNext'] , function(data){
				this.data = data;
				if(successcb) {
					successcb(data);
				}
			}.bind(this) , errorcb);
		}
	} ,
	prev : function(successcb, errorcb){
		if( this.data['linkPrevious']) {
			this.load( this.data['linkPrevious'] , function(data){
				this.data = data;
				if(successcb) {
					successcb(data);
				}
			}.bind(this) , errorcb);	
		}
	} ,
	search : function( keywords , successcb, errorcb) {
		this._get("api/alumni/search?keywords=" + keywords , function(data){
				this.data = data;
				if(successcb) {
					successcb(data);
				}
			}.bind(this) , errorcb);
	} ,
	load : function( link , successcb, errorcb ) {
		this._get(link['href'] , successcb, errorcb);
	} ,
});

(function() {

	var LazyAlumniServiceProvider = Class.extend({
		instance : new LazyAlumniService(),
		$get : [ '$http', function($http) {
			this.instance.$http = $http;
			return this.instance;
		} ]
	})

	angular.module('alumni.LazyAlumniService', []).provider('LazyAlumniService', LazyAlumniServiceProvider);
}());